//
//  FKFindPayPwdVC.m
//  inspectVehicle
//
//  Created by Macbook Pro on 2018/12/12.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKFindPayPwdVC.h"

@interface FKFindPayPwdVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *mobileField;
@property (weak, nonatomic) IBOutlet UITextField *codeField;
@property (weak, nonatomic) IBOutlet UITextField *pwdField;
@property (weak, nonatomic) IBOutlet UITextField *confirmPwdField;
@property (weak, nonatomic) IBOutlet UIButton *getCodeBtn;
@property (weak, nonatomic) IBOutlet UIButton *confirmBtn;

@property (nonatomic, copy) NSString *mobile;

@end

@implementation FKFindPayPwdVC

- (instancetype)init
{
    return [UIStoryboard storyboardWithName:@"FKFindPayPwdVC" bundle:nil].instantiateInitialViewController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    NSDictionary *userInfo = [FKCacheTool userInfo];
    NSString *mobile = userInfo[@"mobile"];
    self.mobile = mobile;
    if (mobile.length == 11) {
        NSString *subStr = [mobile substringWithRange:NSMakeRange(3, 4)];
        self.mobileField.text = [mobile stringByReplacingOccurrencesOfString:subStr withString:@"****"];
    }
}

#pragma mark - UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    NSString *finalText = [textField.text stringByAppendingString:string];
    return [finalText fk_isPureInt] && finalText.length <= 6;
    
//    if ([textField isEqual:self.codeField]) {
//        return [finalText fk_isPureInt] && finalText.length < 6;
//    } else if ([textField isEqual:self.pwdField] || [textField isEqual:self.confirmPwdField]) {
//
//        if (![string isEqualToString:@""]) {
//            if (![string fk_isPureInt]) {
//                return false;
//            }
//            if (textField.text.length >= 6 ) {
//                return false;
//            }
//        }
//    }
//
//    return true;
}

- (IBAction)getCodeBtnClicked:(UIButton *)sender {
    sender.enabled = false;
    
    if (!self.mobile || self.mobile.length != 11) {
        [SVProgressHUD showInfoWithStatus:@"当前用户没有绑定手机号"];
        return;
    }
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"mobile"] = self.mobile;
    [FKHttpTool postWithSuffix:@"m=Home&c=MsgCode&a=sendCode" param:param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            [SVProgressHUD showSuccessWithStatus:@"验证码发送成功"];
            [sender fk_startWithTime:59 title:@"重新发送" countDownTitle:@"秒" normalColor:fkMainColor countdownColor:fkMainColor];
        }
    } failure:nil];
}

- (IBAction)confirmBtnClicked:(UIButton *)sender {
    [self.view endEditing:true];
    
    if (self.codeField.text.length < 1) {
        [SVProgressHUD showInfoWithStatus:self.codeField.placeholder];
        return;
    }
    if (self.pwdField.text.length != 6) {
        [SVProgressHUD showInfoWithStatus:@"请输入6位数的新密码"];
        return;
    }
    if (self.confirmPwdField.text.length != 6) {
        [SVProgressHUD showInfoWithStatus:@"请输入6位数的确认密码"];
        return;
    }
    if (![self.pwdField.text isEqualToString:self.confirmPwdField.text]) {
        [SVProgressHUD showInfoWithStatus:@"新密码与确认密码不一致!"];
        return;
    }
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"mobile"] = self.mobile;
    param[@"password"] = self.pwdField.text;
    param[@"repassword"] = self.confirmPwdField.text;
    param[@"code"] = self.codeField.text;
    param[@"mobile"] = self.mobile;
    [SVProgressHUD show];
    [FKHttpTool postWithSuffix:@"m=Home&c=Mine&a=findPay" param:param success:^(FKBaseResult *result) {
        if (result.isSuccess) {
            [SVProgressHUD dismiss];
            [SVProgressHUD showSuccessWithStatus:@"支付密码修改成功!"];
            if (self.findSuccess) self.findSuccess();
            fkPopVC;
        }
    } failure:nil];
}


@end
